﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bitirme.Controllers;
using Bitirme.DAL.Entities;

namespace Bitirme.Views
{
    public partial class ShowWorkPackages : System.Web.UI.Page
    {
        string projectID;
        int projectid=-1;
        WorkPackageHandler workPackageHandler;
        Project project;
        

        protected void Page_Load(object sender, EventArgs e)
        {
            if (projectid == -1)
            {
                projectID = Request.QueryString["id"];
                projectid = Int32.Parse(projectID);
                ProjectHandler projectHandler = new ProjectHandler();
                workPackageHandler = new WorkPackageHandler();
                project = projectHandler.SelectProjectByID(projectid);
                ProjectName.Text = project.Name;
                ProjectSubject.Text = project.Subject;
                ProjectDetail.Text = project.Details;
               
            }
        }

        protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "update" || e.CommandName == "delete" || e.CommandName == "changestate")
            {
                int rowNum = int.Parse(e.CommandArgument.ToString());
                string choice = e.CommandName;
                string iD = GridView1.Rows[rowNum].Cells[0].Text;
                string state = GridView1.Rows[rowNum].Cells[6].Text;


                if (choice == "changestate") {

                    workPackageHandler = new WorkPackageHandler();
                    workPackageHandler.UpdateWorkPackageStateByID(int.Parse(iD), state);
                    Response.Redirect("ShowWorkPackages.aspx?id=" + projectid);
                }

                else if (choice == "delete")
                {
                    workPackageHandler = new WorkPackageHandler();
                    workPackageHandler.DeletePackageToProjectByWorkPackageID(int.Parse(iD));
                    workPackageHandler.DeletePackageRequirementByWorkPackageID(int.Parse(iD));
                    workPackageHandler.DeleteWorkPackageByID(int.Parse(iD));
                    Response.Redirect("ShowWorkPackages.aspx?id=" + projectid);
                }

            }  

        }

        protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["ProjectID"] = projectid;
        }
    }
}